//
//  HandeARView.m
//  HandeAR
//
//  Created by Tang Boss on 08/12/2017.
//  Copyright © 2017 HandeIdealVison. All rights reserved.
//

#import "HandeARView.h"
#import <Foundation/Foundation.h>
#import "HandeAR+Private.h"
#import "UnityAppController.h"

extern BOOL isAlreadyInitCamera;

@interface HandeARView()
@property (nonatomic, weak) UIView *arView;
@property (nonatomic) ScanType type;
@property (nonatomic, strong) NSDate *date;
@end

@implementation HandeARView
- (id)initWithFrame:(CGRect)frame {
    [UIView setAnimationsEnabled:YES];
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
        self.type = QR_Type;
        self.date = [NSDate date];
        UIView *view = UnityGetGLView();
        if (view) {
            [self addSubview:view];
            self.arView = view;
        }
        if (CGRectIsEmpty(frame)) {
            self.translatesAutoresizingMaskIntoConstraints = NO;
            view.translatesAutoresizingMaskIntoConstraints = NO;
            
            NSDictionary *viewDict = NSDictionaryOfVariableBindings(view);
            [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(0)-[view]-(0)-|" options:0 metrics:nil views:viewDict]];
            [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(0)-[view]-(0)-|" options:0 metrics:nil views:viewDict]];
        }
        else {
            view.frame = [UIScreen mainScreen].bounds;// CGRectMake(0, 0, frame.size.width, frame.size.height);
        }
        
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
    }
    
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
//    self.arView.frame = [UIScreen mainScreen].bounds;
    CGRect rect = [UIScreen mainScreen].bounds;
    self.arView.frame = rect;
}

- (BOOL)showType:(ScanType)type {
    if (!isAlreadyInitCamera) {
        return NO;
    }
    NSDate *now = [NSDate date];
    NSTimeInterval interval = [now timeIntervalSinceDate:self.date];
    if (interval < 0.5) {
        return NO;
    }
    self.date = now;
    switch (type) {
        case QR_Type:
            if (self.type == QR_Type) {
                return NO;
            }
            [[HandeAR sharedInstance] showQR];
            break;
        case AR_Type:
            if (self.type == AR_Type) {
                return NO;
            }
            [[HandeAR sharedInstance] showAR];
            break;
        default:
            break;
    }
    self.type = type;
    
    return YES;
}

- (BOOL)resumeQR {
    if (!isAlreadyInitCamera) {
        return NO;
    }
    NSDate *now = [NSDate date];
    NSTimeInterval interval = [now timeIntervalSinceDate:self.date];
    if (interval < 0.5) {
        return NO;
    }
    self.date = now;
    self.type = QR_Type;
    [[HandeAR sharedInstance] showQR];
    return YES;
}

- (void)applicationDidBecomeActive:(NSNotification *)notice {
    UnityAppController *unityController = (UnityAppController*)[[HandeAR sharedInstance] valueForKey:@"unityController"];
    [unityController applicationDidBecomeActive:[UIApplication sharedApplication]];
}

- (void)willMoveToSuperview:(UIView *)newSuperview {
    if (newSuperview) {
        //enter
        [self applicationDidBecomeActive:nil];
        [[HandeAR sharedInstance] initUnity];
    }
    else {
        //remove
        [[HandeAR sharedInstance] clearUnity];
    }
}


- (void)setHidden:(BOOL)hidden {
    if (hidden) {
        [[HandeAR sharedInstance] clearUnity];
    }
    else {
        [[HandeAR sharedInstance] initUnity];
    }
}

- (void)dealloc {
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center removeObserver:self];
}
@end
